package model;



import java.io.File;
import java.io.IOException;

import java.util.HashSet;

import java.util.Set;

import configuration.Config;
import configuration.ConfigOperator;
import configuration.CreatXMLFile;

/**
 * this cladd contains all the logic behind the GUI 
 * @author ran & barak
 *
 */
public class Model {

	//static member for singelton pattern
	private static Model model=new Model();
	
	/**
	 * @return Simulation instance 
	 */
	public static Model getModel(){
		return model;
	}
	/*
	 * constructor of the class
	 */
	private Model(){
		
		/*ContactService con=new ContactService();
		try {
			remoteInt=con.getRemote();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
	
	/**
	 * send request to login to the drop box service
	 * @param userName 
	 * @param password
	 * @return access denied or not
	 */
	public Boolean login(String userName, String password) {
		System.out.println("try to login...\n");
		System.out.println("name: "+userName+"\n");
		System.out.println("password: "+password+"\n");
		boolean answer=false;
		ConnectToServer checkUser = new ConnectToServer();
		answer = checkUser.checkUser(userName, password);
		//answer=remoteInt.checkUser(userName, password,true);
		
		//access is approved
		if(answer){
			UserInfo.getInstance().setUserName(userName);
			UserInfo.getInstance().setPass(password);
		}
		return answer;
	}
	/**
	 * request to register the drop box service
	 * @param userName
	 * @param password
	 * @param email
	 * @param computerName
	 * @param path the location of drop box folder
	 * @param folderName the first shared folder name, null in case of old client
	 * @return true in case of success in the registration false otherwise
	 */
	public Boolean newAccount(String userName, String password,String email,String computerName,String path, String folderName) {
		System.out.println("enter to DB new client\n");
		System.out.println("name: "+userName+"\n");
		System.out.println("password: "+password+"\n");
		ConnectToServer connectToServer = new ConnectToServer();
		
		//try {
			connectToServer.createAccount(userName, password, email, computerName,path+"\\CleverDropBox\\", folderName);
			//remoteInt.createNewClient(userName, password, email, computerName,path+"\\CleverDropBox\\", folderName);
		/*} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} */
		UserInfo userInfo = UserInfo.getInstance();
		userInfo.setUserName(userName);
		userInfo.setPass(password);
		userInfo.setEmail(email);
		if(folderName!=null){
			Set<String> boxesList = new HashSet<String>();
			boxesList.add(folderName);
			userInfo.setBoxesList(boxesList);
		}
		
		
		
		Config config = new Config();
		config.setUserName(userName);
		config.setPassword(password);
		config.setComputerName(computerName);
		config.setEmail(email);
		config.setDbUserName(userInfo.getDBUserName());
		config.setDbPassword(userInfo.getDBPassword());
		config.setDropBoxLocation(path+"\\CleverDropBox");
		config.setBoxList(userInfo.getBoxesList());
		ConfigOperator.write(config, "C:\\Program Files\\project\\ClientService\\Config.xml");
		/*try {
			//Runtime.getRuntime().exec("cmd /c start c:\\Users\\sl300\\Project\\cleverdropbox\\Service\\run.bat");
			//Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} */
		//openRemotInt();
		
		System.out.println(path+"\\CleverDropBox");
		//create cleverDropBox folder in the path specified
		File f = new File(path+"\\CleverDropBox");
		try{
			if(f.mkdir())
				System.out.println("Directory Created");
			else
				System.out.println("Directory is not created");
		}catch(Exception e){
			e.printStackTrace();
		}
		//new client create the folders
		if(folderName!=null){
			//create first shared folder called: folderName 
			System.out.println(path+"\\CleverDropBox\\"+folderName);
			File f1 = new File(path+"\\CleverDropBox\\"+folderName);
			try{
				if(f1.mkdir())
					System.out.println("Directory Created");
				else
					System.out.println("Directory is not created");
				//remoteInt.startWatcher(path+"\\CleverDropBox\\"+folderName);
			}catch(Exception e){
				e.printStackTrace();
			}
		//exist client
		}else{
			System.out.println("WTF???");
			/*try {
				Set<String> folders=remoteInt.getBoxesList();
				//get all client folders & create them in the computer that the application installed
				for(String dir:folders){
					File f1 = new File(path+"\\CleverDropBox\\"+dir);
					if(f1.mkdir())
						System.out.println("Directory Created");
					else
						System.out.println("Directory is not created");
					//remoteInt.startWatcher(path+"\\CleverDropBox\\"+dir);
					
					
				}
			}catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} */
			
		}
		CreatXMLFile creatXMLFile = new CreatXMLFile();
		creatXMLFile.createDbAndHibernateXml("ClientDatabase", userInfo.getDBUserName(), userInfo.getDBPassword());
		try {
			Runtime.getRuntime().exec("cmd /c start ClientService\\run.bat");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	/**
	 * check if user exist already
	 * @param userName
	 * @param password
	 * @param email
	 * @return success or failure
	 */
	public Boolean checkUser(String userName, String password, String email) {
		boolean answer=false;
		ConnectToServer checkUser = new ConnectToServer();
		
			//answer=remoteInt.checkUser(userName, password,false);
			answer = checkUser.checkUser(userName, password);
		
		//the user is not exist
		if(!answer){
			UserInfo.getInstance().setUserName(userName);
			UserInfo.getInstance().setPass(password);
			UserInfo.getInstance().setEmail(email);
		}	
		return answer;
	}
	public void setDbSettings(String dbUserName, String dbPassword) {
		System.out.println("Model : setDbSettings");
		/*try {
			remoteInt.setDbSettings(dbUserName,dbPassword);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} */
		UserInfo.getInstance().setDBUserName(dbUserName);
		UserInfo.getInstance().setDBPassword(dbPassword);
	}

}
